package org.saltlux.study.jackrabbit;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.jcr.ImportUUIDBehavior;
import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.junit.Test;

public class ThirdHopTester {

	/**
	 * - test.xml을 읽어서 repository에 넣는다. - test.xml에 있는 내용이 repository에 있는지 확인한다.
	 * ( xhtml:html/xhtml:body/xhtml:p/Last Modified January 10, 2002 )
	 * @throws RepositoryException 
	 * @throws LoginException 
	 * @throws IOException 
	 */
	@Test
	public void testThirdHop() throws LoginException, RepositoryException, IOException {
		Repository repository = RepositoryManager.getInstance().getRepository();
		Session session = repository.login(new SimpleCredentials("username",
				"password".toCharArray()));

		Node root = session.getRootNode();
		if(!root.hasNode("test.xml")) {
			Node testXml = root.addNode("test.xml");
			
			FileInputStream fis = new FileInputStream(new File("test.xml"));
			session.importXML(testXml.getPath(), fis, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
			
			try {
				fis.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			session.save();
		}
		
		Node node = root.getNode("test.xml/xhtml:html/xhtml:body/xhtml:p[2]/jcr:xmltext");
		Property property = node.getProperty("jcr:xmlcharacters");
		System.out.println(property.getString());
		
//		NodeIterator nodes = root.getNode("test.xml").getNodes();
//		while(nodes.hasNext()) {
//			Node eachNode = (Node) nodes.next();
//			System.out.println(eachNode.getPath());
//		}
	}

}
